/**
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑, 永无BUG!
 * 　　　　┃　　　┃Code is far away from bug with the animal protecting
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 */
package com.zkgengkun.console.controller;

import com.zkgengkun.console.service.BlogService;
import com.zkgengkun.console.service.GroupService;
import com.zkgengkun.constant.SysConstants;
import com.zkgengkun.domain.Blog;
import com.zkgengkun.domain.Group;
import com.zkgengkun.support.component.DynamicDataSource;
import com.zkgengkun.support.enums.DataSourceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author gengshikun
 * @date 2016/12/15
 */
@Controller
@RequestMapping(value = "admin/blog")
public class BlogController {

    private static final Logger logger = LoggerFactory.getLogger(BlogController.class);

    @Autowired
    private BlogService blogService;

    @Autowired
    private GroupService groupService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(String query, Integer pageNo, Model model) {
        Page pager = this.blogService.getListForPage(query, pageNo);
        model.addAttribute("pager", pager.getContent());
        model.addAttribute("pageSize", pager.getTotalPages());
        model.addAttribute("pageNo", pageNo == null ? 1 : pageNo);
        return "console/blog/list";
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add(Model model) {
        List<Group> groups = this.groupService.getList(SysConstants.GROUP_TYPE_BLOG);
        model.addAttribute("groups", groups);
        return "console/blog/add";
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(Blog blog) {
        this.blogService.save(blog);
        return "redirect:list";
    }

    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String edit(@PathVariable String id, Model model) {
        Blog blog = this.blogService.getOne(id);
        model.addAttribute("blog", blog);
        model.addAttribute("groups", this.groupService.getList(SysConstants.GROUP_TYPE_BLOG));
        return "console/blog/edit";
    }

    @RequestMapping(value = "save_and_edit", method = RequestMethod.POST)
    public String saveAndEdit(Blog blog){
        this.blogService.saveAndEdit(blog);
        return "redirect:list";
    }
}
